package is.web.htgen.multiple;

import is.bean.department.*;
import is.bean.employee.EmployeeBeanRemoteHome;
import is.bean.region.RegionBeanRemoteHome;
import is.web.*;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import javax.ejb.FinderException;

/**
 *
 * @author aspr0
 */
public class DepartmentTable
{
    private Collection<DepartmentBeanRemote> departments;
    private DepartmentBeanRemoteHome departmentHome;
    private EmployeeBeanRemoteHome employeeHome;
    private RegionBeanRemoteHome regionHome;
    private Department findExample;

    public void setDepartmentHome(DepartmentBeanRemoteHome departmentHome)
    {
        this.departmentHome = departmentHome;
    }

    public void setDepartments(Collection<DepartmentBeanRemote> departments)
    {
        this.departments = departments;
    }

    public void setEmployeeHome(EmployeeBeanRemoteHome employeeHome)
    {
        this.employeeHome = employeeHome;
    }

    public void setFindExample(Department findExample)
    {
        this.findExample = findExample;
    }

    public void setRegionHome(RegionBeanRemoteHome regionHome)
    {
        this.regionHome = regionHome;
    }

    public String generate() throws RemoteException, FinderException
    {
        String table =
                "<table>"
                + "<thead>%s</thead>"
                + "<tbody>%s%s</tbody>"
                + "</table>";
        String header =
                "<tr>"
                + "<td>Id</td>"
                + "<td>Name</td>"
                + "<td>Parent department</td>"
                + "<td>Boss</td>"
                + "<td>Region</td>"
                + "<td>Tools</td>"
                + "</tr>";
        String findId = NullUtil.toString(findExample.getId());
        String findName = NullUtil.toString(findExample.getName());
        String findForm =
                "<tr>"
                + "<form method=\"post\">"
                + "<td><input type=\"text\" name=\"id\" value=\"" + findId + "\" class=\"find-id\"></td>"
                + "<td><input type=\"text\" name=\"name\" value=\"" + findName + "\" class=\"find-wide\"></td>"
                + "<td></td>"
                + "<td></td>"
                + "<td></td>"
                + "<td>"
                + "<input type=\"hidden\" name=\"find\" value=\"find\">"
                + "<input type=\"submit\" value=\"find\"></td>"
                + "</form>"
                + "</tr>";
        String body = "";


        Iterator<DepartmentBeanRemote> i = departments.iterator();

        while (i.hasNext())
        {
            Department currentDepartment = i.next().getData();

            Integer id = currentDepartment.getId();
            String name = currentDepartment.getName();
            Integer parentId = currentDepartment.getParentId();
            Integer bossId = currentDepartment.getBossId();
            Integer regionId = currentDepartment.getRegionId();


            String departmentLinkCanvas = "<a href=\"../department?id=%s&act=retrieve\">%s</a>";
            String parentLink = (Checker.isPositiveInteger(parentId))
                    ? String.format(departmentLinkCanvas,
                                    parentId,
                                    departmentHome.findByPrimaryKey(parentId).getData().getName())
                    : Locale.NULL_READABLE;

            String employeeLinkCanvas = "<a href=\"../employee?id=%s&act=retrieve\">%s</a>";
            String bossLink = (Checker.isPositiveInteger(bossId))
                    ? String.format(employeeLinkCanvas,
                                    bossId,
                                    employeeHome.findByPrimaryKey(bossId).getData().getName())
                    : Locale.NULL_READABLE;

            String regionLinkCanvas = "<a href=\"../region?id=%s&act=retrieve\">%s</a>";
            String regionLink = String.format(regionLinkCanvas,
                                              regionId,
                                              regionHome.findByPrimaryKey(regionId).getData().getName());

            String row =
                    "<tr>"
                    + "<td>" + id + "</td>"
                    + "<td>" + name + "</td>"
                    + "<td>" + parentLink + "</td>"
                    + "<td>" + bossLink + "</td>"
                    + "<td>" + regionLink + "</td>"
                    + "<td>"
                    + "<a href=\"?id=" + id + "&act=retrieve\" title=\"retrieve\">"
                    + "<image src=\"../img/document-open.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=update\" title=\"update\">"
                    + "<image src=\"../img/document-properties.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=copy\" title=\"copy\">"
                    + "<image src=\"../img/edit-copy.png\" class=\"hMargin\"></a>"
                    + "<a href=\"?id=" + id + "&act=delete\" title=\"delete\">"
                    + "<image src=\"../img/edit-delete.png\" class=\"hMargin\"></a>"
                    + "</td>"
                    + "</tr>";
            body += row;
        }

        return String.format(table, header, findForm, body);

    }
}
